package com.chefu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.client.server.ServerClient;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.pojo.R;
import com.chefu.server.mapper.ServerClassifyMapper;
import com.chefu.server.pojo.ServerClassify;
import com.chefu.server.service.ServerClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@Service
public class ServerClassifyServiceImpl extends ServiceImpl<ServerClassifyMapper, ServerClassify> implements ServerClassifyService {
    @Autowired
    private ServerClassifyMapper serverClassifyMapper;

    @Override
    public void saveServerClassify(Long code, String name) {
        ServerClassify serverClassify = new ServerClassify();
        serverClassify.setScId(code);
        serverClassify.setName(name);
        serverClassify.setParentId(0L);

        serverClassifyMapper.insert(serverClassify);
    }

    @Override
    public PageResult<ServerClassify> serverClassifyPageQuery(Integer pageNum, Integer pageSize, Long code, String name) {

        //封装分页参数
        IPage<ServerClassify> iPage = new Page<>(pageNum,pageSize);

        //封装查询条件
        QueryWrapper<ServerClassify> queryWrapper = Wrappers.query();

        //***把需要的条件封装进去QueryWrapper里面去***
        if(code!=null){
            queryWrapper.like("sc_id",code);
        }
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }

        //2.查询数据，获取结果
        iPage = serverClassifyMapper.selectPage(iPage,queryWrapper);

        //3.处理(封装)结果
        PageResult<ServerClassify> pageResult = new PageResult<>(iPage.getTotal(),iPage.getSize(),iPage.getRecords());

        return pageResult;
    }

    @Override
    public void updateServerClassify(Long id,String name) {

        ServerClassify serverClassify = findServerClassify(id);
        serverClassify.setName(name);

        serverClassifyMapper.updateById(serverClassify);
    }

    @Override
    public ServerClassify findServerClassify(Long id) {
        ServerClassify serverClassify = serverClassifyMapper.selectById(id);
        return serverClassify;
    }

    @Override
    public String deleteServerClassify(Long id) {
        //通过服务分类id查询服务分类
        ServerClassify serverClassify = findServerClassify(id);

        //通过服务分类父id查询服务子分类
        List<ServerClassify> serverClassifyList = sonClassifyByParentId(serverClassify.getParentId());

        //判断子分类是否为空，为空则可删除
        if(CollectionUtils.isEmpty(serverClassifyList)){
            serverClassifyMapper.deleteById(serverClassify);
        }

        //不为空则不能删除
        return "删除失败";

    }

    @Override
    public List<ServerClassify> sonClassifyByParentId(Long parentId) {
        ServerClassify serverClassify = new ServerClassify();
        serverClassify.setParentId(parentId);

        //封装条件
        QueryWrapper<ServerClassify> queryWrapper = Wrappers.query(serverClassify);

        //封装查询结果
        List<ServerClassify> serverClassifyList = serverClassifyMapper.selectList(queryWrapper);

        //返回结果
        return serverClassifyList;
    }

    @Override
    public void saveSonClassifyByParentId(Long parentId, Long code, String name, Integer generic) {
        ServerClassify serverClassify = new ServerClassify();
        serverClassify.setParentId(parentId);
        serverClassify.setScId(code);
        serverClassify.setName(name);
        serverClassify.setScGeneric(generic);

        //封装结果
        serverClassifyMapper.insert(serverClassify);

    }

    @Override
    public PageResult<ServerClassify> sonClassifyPageQuery(Integer pageNum, Integer pageSize, Long code, String name) {
        //封装分页参数
        IPage<ServerClassify> iPage = new Page<>(pageNum,pageSize);

        //封装查询条件
        QueryWrapper<ServerClassify> queryWrapper = Wrappers.query();

        //***把需要的条件封装进去QueryWrapper里面去***
        if(code!=null){
            queryWrapper.like("sc_id",code);
        }
        if(name!=null){
            queryWrapper.like("name",name);
        }

        //2.查询数据，获取结果
        iPage = serverClassifyMapper.selectPage(iPage,queryWrapper);

        //3.处理(封装)结果
        PageResult<ServerClassify> pageResult = new PageResult<>(iPage.getTotal(),iPage.getSize(),iPage.getRecords());

        return pageResult;
    }

    @Override
    public ServerClassify sonClassifyById(Long id) {
        ServerClassify serverClassify = serverClassifyMapper.selectById(id);
        return serverClassify;
    }

    @Override
    public void updateSonClassify(Long id,String name) {
        ServerClassify serverClassify = sonClassifyById(id);
        serverClassify.setName(name);
        serverClassifyMapper.updateById(serverClassify);
    }

    @Override
    public void deleteSonClassifyById(Long id) {
        ServerClassify serverClassify = new ServerClassify();
        serverClassify.setScId(id);

        //删除
        serverClassifyMapper.deleteById(serverClassify);
    }
}
